class Admin::SalesFiguresController < Admin::BaseController
  include PaginatorHelper
  
  def index
    parameters = create_parameters(params)
    
    @products = Admin.get_products_statistics(parameters)
    if(@products.size> 0 && @products != nil)
      @total_stats = Statistics.get_summed_result(@products)
    end
    if(params[:page] != nil) then
      @page = params[:page].to_i
    else
      @page = 0 
    end
    hash_product_list = get_shortened_list(@products,@page)
    @products = hash_product_list[:object]
    @indices = hash_product_list[:indices]
  end
  
  
  private
  #creeert de parameters die worden meegegeven om de textvelden in de index ingevuld te houden.
  def create_parameters(params)
    parameters = {:search=>{:and=>{:CATEGORY => "",:DATE=>{'orders.created_at' =>{:gt => nil, :st => nil}}}}}
    @params = {}
    if(params == nil)
      params = {}
    end
    if(params[:category] != nil)
      parameters[:search][:and][:CATEGORY] = params[:category]
      @params[:category] = params[:category]
      @category = params[:category]
    end
    if(params[:to] != nil)
      parameters[:search][:and][:DATE]['orders.created_at'][:st] = params[:to]
      date = params[:to]
      if(Date.valid_civil?(date[:year].to_i,date[:month].to_i,date[:day].to_i))
          @date_to = Date.new(date[:year].to_i,date[:month].to_i,date[:day].to_i)
      end
      @params[:to] = params[:to]
    end
    if(params[:from] != nil)
      parameters[:search][:and][:DATE]['orders.created_at'][:gt] = params[:from]
      date = params[:from]
      if(Date.valid_civil?(date[:year].to_i,date[:month].to_i,date[:day].to_i))
          @date_from = Date.new(date[:year].to_i,date[:month].to_i,date[:day].to_i)
      end
      @params[:from] = params[:from]
    end
    if(params[:sort] != nil)
      parameters[:sort] = params[:sort]
      @params[:sort] = params[:sort]
    end
    if(params[:sort_on] != nil)
      parameters[:sort_on] = params[:sort_on]
      @params[:sort_on] = params[:sort_on]
    end
    if(params[:page] != nil)
      @params[:page] = params[:page]
    end
    return parameters
  end
end